/**
 * 数据库连接
 */

const mysql = require('mysql')

const { MYSQL_CONF } = require('../config/db')

const con = mysql.createConnection(MYSQL_CONF)

con.on('error', () => {
    console.log('数据库连接已断开，请重启...')
})

const execSql = (sql) => {
    const promise = new Promise((resolve, reject) => {
        con.query(sql, (err, data) => {
            if (err) {
                reject(err)
                return
            }
            // console.log('data', data)
            resolve(data)
        })
    })

    return promise
}

module.exports = {
    execSql
}